edibility={}
edibility["melon"]=10

gunworldtoweap={}
gunworldtoweap["glock_world"]="weap_glock"
gunworldtoweap["super_shotgun_world"]="weap_pumpshotgun"
gunworldtoweap["mac10_world"]="weap_mac10"

gunworldtoweap["axe_world"]="axe"

function eatstuff(ply,command,args)
	local stufftoeat=args[1]
	local howmany=tonumber(args[2])
	local maxhealth=100
	
	if(ply.inventory[stufftoeat]==nil) then
		ply.inventory[stufftoeat]=0
	end

	if(ply.inventory[stufftoeat]<howmany) then
		ply:ChatNotify("You don't have "..howmany.." ("..stufftoeat..") to eat. You have "..ply.inventory[stufftoeat])
		return
	elseif(ply:Health()+howmany*edibility[stufftoeat]>maxhealth) then
		ply:ChatNotify("You will waste some "..stufftoeat..". If you eat "..howmany.." out of "..ply.inventory[stufftoeat])
		return
	else
		ply:SetHealth(ply:Health()+edibility[stufftoeat]*howmany)
		ply.inventory[stufftoeat]=ply.inventory[stufftoeat]-howmany
		ply:ChatNotify("You ate "..howmany.." ("..stufftoeat..") and gained "..edibility[stufftoeat]*howmany.." life")
		//needs hunger
	end
end

concommand.Add("Eat", eatstuff)

function dropstuff(ply,command,args)
	//begin trace to spawnpos
	local tracedata={}
	tracedata.start=ply:GetShootPos()
	tracedata.endpos=ply:GetShootPos()+ply:GetAimVector()*80
	tracedata.filter=ply
	local trace=util.TraceLine(tracedata)
	local spawnvector= ply:GetPos()
	if(!trace.Hit) then
		spawnvector=trace.HitPos
	else
		spawnvector=trace.HitPos+trace.HitNormal*3
	end
	//end spawnpos trace, spawn at spawnvector


	local stufftodrop=args[1]
	local howmany=tonumber(args[2])
	

	if(args[2]=="all") then
		howmany=ply.inventory[stufftodrop]
	end

	if(howmany> 20 && stufftodrop!="money") then
		ply:Chat("This server limits max drops to 20")
		howmany=20
	end
	if(ply.inventory[stufftodrop]<howmany) then
		ply:ChatNotify("You don't have "..howmany.." ("..stufftodrop..") to drop. You have "..ply.inventory[stufftodrop])
		return
	else
		
		if(stufftodrop=="money") then
			local moneydrop=ents.Create(stufftodrop)
			moneydrop:SetPos(spawnvector)
			moneydrop.Cash=howmany
			moneydrop:Spawn()
			ply.inventory[stufftodrop]=ply.inventory[stufftodrop]-howmany
			ply:ChatNotify("You dropped "..howmany.." ("..stufftodrop..")")
		else
			local dropped=0
			local start=CurTime()
			local spawnvector2=spawnvector
			for i=1,howmany do
				timer.Simple(0.2*i,function()
					local tries=0
					while(#ents.FindInSphere(spawnvector2,1)!=0 && tries<20) do
						tries=tries+1
						spawnvector2=Vector(spawnvector2.x,spawnvector2.y,spawnvector2.z+1)
						if(spawnvector2.z==spawnvector.z+10) then
							spawnvector2.x=spawnvector.x+math.random(-30,30)
							spawnvector2.y=spawnvector.y+math.random(-30,30)
							spawnvector2.z=spawnvector.z
						end
					end
					dropped=dropped+1
					local dropped= ents.Create(stufftodrop)
					dropped:SetPos(spawnvector2)
					dropped:Spawn()
					ply.inventory[stufftodrop]=ply.inventory[stufftodrop]-1
				end)	
			end
			timer.Simple(0.2*howmany+1, function()
				ply:ChatNotify("You dropped "..dropped.." ("..stufftodrop..")")
			end)
		end
	end
	// haha funny hackery XD
	if(ply:GetActiveWeapon()!=nil && ply:GetActiveWeapon().Name!=nil) then
		ply:GetActiveWeapon():Deploy()
	end
end

concommand.Add("Drop", dropstuff)

function equipguns(ply,command,args)
	local gunentity=args[1]
	local guntoequip=gunworldtoweap[gunentity]

	if(ply.inventory[gunentity]<1) then
		ply:ChatNotify("You have insufficient ".. gunentity)
		return
	elseif(ply:GetWeapon(guntoequip)!=NULL) then
		print(ply:GetWeapon(guntoequip))
		ply:ChatNotify("You already have "..guntoequip.." equipped")
		return 
	else 
		ply.inventory[gunentity]=ply.inventory[gunentity]-1
		ply:Give(guntoequip)
		ply:ChatNotify(guntoequip.." equipped")
	end
end

concommand.Add("Equip", equipguns)	

function loadcrate(ply,command,args)
	local toload=string.Explode(" ",args[1])[1]
	local into=string.Explode(" ",args[1])[2]
	local amount=tonumber(args[2])

	if(ply.inventory[toload]==nil) then
		ply.inventory[toload]=0
	end

	if(args[2]=="all") then amount=ply.inventory[toload] end
	if(ply.inventory[toload]<amount) then
		ply:ChatNotify("You don't have "..howmany.." ("..toload..") to place into this crate. You have "..ply.inventory[toload])
		return
	else
		local crate= ents.GetByIndex(into)
		if(crate.contents==nil) then ply:ChatNotify("Not a Valid Crate") return end
		ply.inventory[toload]=ply.inventory[toload]-amount
		
		if(crate.contents[toload]==nil) then crate.contents[toload]=0 end
		crate.contents[toload]=crate.contents[toload]+amount
	
		ply:ChatNotify("You Placed "..amount.." "..toload.." into the crate")
	end


end

concommand.Add("PlaceInCrate", loadcrate)	

function takefromcrate(ply,command,args)
	local toload=string.Explode(" ",args[1])[1]
	local from=string.Explode(" ",args[1])[2]
	local amount=tonumber(args[2])
	local crate= ents.GetByIndex(from)

	if(ply.inventory[toload]==nil) then
		ply.inventory[toload]=0
	end

	if(args[2]=="all") then amount=crate.contents[toload] end

	if(crate.contents[toload]==nil) then crate.contents[toload]=0 end

	if(crate.contents[toload]<amount) then
		ply:ChatNotify("The crate doesn't "..howmany.." ("..toload..") to take. It Has "..crate.contents[toload])
		return
	else
		
		if(crate.contents==nil) then ply:ChatNotify("Not a Valid Crate") return end
		ply.inventory[toload]=ply.inventory[toload]+amount
		
		
		crate.contents[toload]=crate.contents[toload]-amount
	
		ply:ChatNotify("You Took "..amount.." "..toload.." from the crate")
	end
end
concommand.Add("Take",takefromcrate)

function buildcrate(ply,command,args)
	local amount=10
	if(ply.inventory["wood"]==nil) then
		ply.inventory["wood"]=0
	end


	if(ply.inventory["wood"]<amount) then
		ply:ChatNotify("You don't have enough wood ("..ply.inventory["wood"]..") to make a crate.("..amount..")")
		return
	else
		
		ply.inventory["wood"]=ply.inventory["wood"]-amount	
		
		local crate= ents.Create("transportcrate")
		local tracedata={}
		tracedata.start=ply:GetShootPos()
		tracedata.endpos=ply:GetShootPos()+ply:GetAimVector()*80
		tracedata.filter=ply
		local trace=util.TraceLine(tracedata)
		local spawnvector= ply:GetPos()
		if(!trace.Hit) then
			spawnvector=trace.HitPos
		else
			spawnvector=trace.HitPos+trace.HitNormal*3
		end

		crate:SetPos(spawnvector)
		crate:Spawn()
	
		ply:ChatNotify("You Made A Crate")
	end
end
concommand.Add("BuildCrate",buildcrate)